home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.08 Aug 90 / TEDemo Source
Encoding:
Text File  |  1990-07-11  |  26.9 KB  |  949 lines  |  [TEXT/nX^n]

  1. Listing 1: TEDemo.h (header file)
  2.  
  3. class TTEApplication : public TApplication {
  4. public:
  5.     virtual pascal void 
  6.         ITEApplication(OSType itsMainFileType);
  7.     virtual pascal struct TDocument 
  8.         *DoMakeDocument(CmdNumber itsCmdNumber);
  9.     virtual pascal struct TCommand 
  10.         *DoMenuCommand(CmdNumber aCmdNumber);
  11.     virtual pascal void PoseModalDialog();
  12. #ifdef qDebug
  13.     virtual pascal void IdentifySoftware();
  14. #endif
  15. };
  16.  
  17. class TTEDocument;
  18.  
  19. class TPaletteView : public TView {
  20.  
  21. public:
  22.     int        fIconSelected;
  23.     virtual pascal void 
  24.         IPaletteView(TTEDocument *itsTEDocument);
  25.     virtual pascal struct TCommand 
  26.         *DoMouseCommand(Point *theMouse,
  27.                     EventInfo *info, Point *hysteresis);
  28.     pascal void DoHighlightSelection
  29.                     (HLState fromHL, HLState toHL);
  30.     pascal void Draw(Rect *area);
  31. #ifdef qDebug
  32.     virtual pascal void Fields(pascal void (*DoToField)
  33.          (StringPtr fieldName, Ptr fieldAddr, 
  34.             short fieldType, void *link), void *link);
  35. #endif
  36. };
  37.  
  38. class TBox : public TObject {
  39. public:
  40.     Rect    fLocation;
  41.     pascal void IBox(Rect *itsLocation);
  42.     pascal void DrawShape();
  43.     pascal void NeedDiskSpace(long *data);
  44.     pascal void Read(short aRefNum);
  45.     pascal void Write(short aRefNum);
  46. #ifdef qDebug
  47.     virtual pascal void Fields(pascal void (*DoToField)
  48.          (StringPtr fieldName, Ptr fieldAddr, 
  49.             short fieldType, void *link), void *link);
  50. #endif
  51. };
  52.  
  53. class TTextView : public TTEView {
  54.  
  55. public:
  56.     TTEDocument    *fTEDocument;
  57.     TPaletteView    *fPaletteView;
  58.     Boolean            fUpdated;
  59.     pascal void 
  60.         ITextView(TTEDocument *itsTEDocument);
  61.     pascal Boolean DoIdle(IdlePhase phase);
  62.     pascal struct TCommand  *DoKeyCommand(short ch, 
  63.                 short aKeyCode, EventInfo *info);
  64.     pascal struct TCommand 
  65.         *DoMenuCommand(CmdNumber aCmdNumber);
  66.     pascal void DoSetupMenus ();
  67.     pascal struct TCommand 
  68.         *DoMouseCommand(Point *theMouse, 
  69.             EventInfo *info, Point *hysteresis);
  70.     pascal Boolean DoSetCursor
  71.             (Point localPoint, RgnHandle cursorRgn);
  72.     pascal void Draw(Rect *area);
  73. #ifdef qDebug
  74.     virtual pascal void Fields(pascal void (*DoToField)
  75.          (StringPtr fieldName, Ptr fieldAddr, 
  76.             short fieldType, void *link), void *link);
  77. #endif
  78. };
  79.  
  80. class TColorCmd : public TCommand {
  81.  
  82. public:
  83.     TTEDocument    *fTEDocument;
  84.     TTextView        *fTextView;
  85.     int    fOldColorCmd, fNewColorCmd;
  86.     pascal void IColorCmd (int aCmdNumber, 
  87.         TTEDocument *itsDocument,TTextView *itsView);
  88.     pascal void DoIt();
  89.     pascal void RedoIt();
  90.     pascal void UndoIt();
  91. #ifdef qDebug
  92.     virtual pascal void Fields(pascal void (*DoToField)
  93.          (StringPtr fieldName, Ptr fieldAddr, 
  94.             short fieldType, void *link), void *link);
  95. #endif
  96. };
  97.  
  98. class TSketcher : public TCommand {
  99.  
  100. public:
  101.     TTEDocument     *fTEDocument;
  102.     TTextView        *fTextView;
  103.     TBox    *fBox;        // the object being sketched
  104.     Rect fBoxLocation; // size of the Box being sketched
  105.     pascal void ISketcher
  106.     (TTEDocument *itsDocument, TTextView *itsView);
  107.     pascal struct TCommand *TrackMouse
  108.         (TrackPhase aTrackPhase, VPoint *anchorPoint, 
  109.             VPoint *previousPoint, VPoint *nextPoint, 
  110.             Boolean mouseDidMove);
  111.     pascal void DoIt();
  112.     pascal void RedoIt();
  113.     pascal void UndoIt();
  114. #ifdef qDebug
  115.     virtual pascal void Fields(pascal void (*DoToField)
  116.          (StringPtr fieldName, Ptr fieldAddr, 
  117.             short fieldType, void *link), void *link);
  118. #endif
  119. };
  120.  
  121. class TTEDocument : public TDocument {
  122. public:
  123.     TPaletteView    *fPaletteView;
  124.     TTextView    *fTextView;
  125.     Handle        fTextHdl;    // text typed by user
  126.     int    fTextColorCmd;        // menu command number
  127.     TList *fShapeList;    // list of Shapes to be drawn
  128.     pascal void ITEDocument();
  129.     pascal void AddShape(TBox *aBox); 
  130.     pascal void DeleteShape();
  131.     pascal void DoMakeWindows();
  132.     pascal void DoMakeViews(Boolean forPrinting);
  133.     pascal void DoNeedDiskSpace
  134.         (long *dataForkBytes, long *rsrcForkBytes);
  135.     pascal void DoRead(short aRefNum, 
  136.         Boolean rsrcExists, Boolean forPrinting);
  137.     pascal void DoWrite(short aRefNum, 
  138.         Boolean makingCopy);
  139.     pascal void ForEachShapeDo
  140.         (pascal void (*DoToItem) (TObject *item, void 
  141.     *DoToItem_Staticlink),void *DoToItem_Staticlink);
  142.     pascal void Free();
  143.     pascal void SetTextColorCmd(int theColorCmd);
  144.     pascal int  TextColorCmd();
  145. #ifdef qDebug
  146.     virtual pascal void Fields(pascal void (*DoToField)
  147.          (StringPtr fieldName, Ptr fieldAddr, 
  148.             short fieldType, void *link), void *link);
  149. #endif
  150. };
  151.  
  152. // --- global definitions ---
  153.  
  154. typedef pascal void (*DoToObject) 
  155.         (TObject *aObject, void *DoToObject_staticlink);
  156. struct CalcDiskSpaceStruct {  long myDataForkBytes; };
  157. struct DoToRectStruct {  int myRefNum; };
  158.  
  159. //
  160. // --- end of declarations ---
  161. //
  162.  
  163. Listing 2: TEDemo.cp (implementation)
  164.  
  165. #include <UMacApp.h>
  166. #include <UPrinting.h>
  167. #include <UTEView.h>
  168. #include <Fonts.h>
  169. #include <ToolUtils.h>
  170.  
  171. #include "TEDemo.h"
  172.  
  173. const OSType kSignature     = 'JLMT';
  174. const OSType kFileType     = 'JL01';
  175. const int    kWindID        =    1001;    
  176. const int    kHelpID         =     1001;    // Help DLOG ID 
  177. const int    kPaletteWidth    =    640;
  178. const int    kPaletteHeight    =    32;
  179. const int    kIconWidth        =    32;
  180. const int    kBoxIconID        =    256;
  181. const int    kTextIconID        =    257;
  182. const int    kBox            =    1;
  183. const int    kText            =    2;
  184. const int    kPaletteColor     =    greenColor;
  185. const int    kBoxColor        =    redColor;
  186.  
  187. // commands
  188. const int    cHelp         =    1001;    // for color menu items
  189. const int    cDrawBox    =    2001;    
  190.             // command number for Box sketcher object
  191.  
  192. const int    cBlack         =    5001;    // for TextColor menu
  193. const int    cBlue         =    5002;
  194. const int    cGreen         =    5003;
  195. const int    cRed         =    5004;
  196. const int    cWhite         =    5005;
  197.        
  198. int        gColorArray[cWhite-cBlack+1];
  199. Rect    gIconRect[kText-kBox+1];
  200.  
  201. pascal void TTEApplication::ITEApplication
  202.                     (OSType itsMainFileType)
  203. {     gColorArray[cBlack-cBlack]    = blackColor; 
  204.     gColorArray[cBlue-cBlack]     = blueColor;
  205.     gColorArray[cGreen-cBlack]    = greenColor;
  206.     gColorArray[cRed-cBlack]     = redColor;
  207.     gColorArray[cWhite-cBlack]    = whiteColor;
  208.     SetRect(&gIconRect[kBox-kBox], 
  209.                         0, 0, kIconWidth, kIconWidth);
  210.     SetRect(&gIconRect[kText-kBox], kIconWidth, 
  211.                         0, 2 * kIconWidth, kIconWidth);
  212.     IApplication(itsMainFileType);
  213.     RegisterStdType("\pTTextView", 'text');
  214.     if (gDeadStripSuppression)
  215.     {    TTextView *aTextView;
  216.         aTextView = new TTextView;    }
  217.     InitPrinting();
  218. }
  219.  
  220. pascal struct TDocument 
  221.     *TTEApplication::DoMakeDocument
  222.                     (CmdNumber itsCmdNumber)
  223. {    TTEDocument* aTEDocument;
  224.     aTEDocument = new TTEDocument;
  225.     FailNIL(aTEDocument);
  226.     aTEDocument->ITEDocument();
  227.     return aTEDocument;        }
  228.  
  229. pascal void TTEApplication::PoseModalDialog()
  230. {    DialogPtr dPtr;
  231.     short    dItem;
  232.     dPtr = GetNewDialog(kHelpID, nil, (WindowPtr) -1);
  233.     ModalDialog(nil, &dItem);
  234.     DisposDialog(dPtr);    }
  235.  
  236.  
  237. pascal struct TCommand 
  238.         *TTEApplication::DoMenuCommand
  239.                         (CmdNumber aCmdNumber)
  240. {    switch (aCmdNumber) {
  241.         case cHelp: 
  242.             PoseModalDialog(); 
  243.             return gNoChanges;
  244.         default:    
  245.     return inherited::DoMenuCommand(aCmdNumber); }
  246. }
  247.  
  248. #ifdef qDebug
  249. pascal void TTEApplication::IdentifySoftware()
  250. {    ProgramReport
  251. ("\pTEDemo ©J.Langowski/MacTutor June 1990",false);
  252.     inherited::IdentifySoftware();    }
  253. #endif
  254.  
  255. // ---- Document ----
  256.  
  257. pascal void TTEDocument::ITEDocument()
  258. {    TList    *aList;
  259.     IDocument(kFileType, kSignature, kUsesDataFork,
  260.             !kUsesRsrcFork, !kDataOpen, !kRsrcOpen);
  261.     fSavePrintInfo = true;    // save print info in data fork
  262.     fTextHdl = NewPermHandle(0);    // heap block for text
  263.     FailNIL(fTextHdl);
  264.     fTextColorCmd = cBlack;
  265.     aList = NewList();    // make empty list of Boxs
  266.     fShapeList = aList;    }
  267.  
  268. pascal void TTEDocument::AddShape(TBox *aBox)
  269. {    fShapeList->InsertFirst(aBox);        }
  270.  
  271. pascal void TTEDocument::DeleteShape()
  272. {    fShapeList->Delete(fShapeList->First());  }
  273.  
  274. pascal void TTEDocument::DoMakeViews
  275.                             (Boolean forPrinting)
  276. {    const Boolean kSquareDots = true;
  277.     const Boolean kFixedSize = true;
  278.     TPaletteView        *aPaletteView;
  279.     TTextView        *aTextView;
  280.     TStdPrintHandler    *aStdHandler;
  281.     aPaletteView = new TPaletteView;
  282.     FailNIL(aPaletteView);
  283.     aPaletteView->IPaletteView(this);
  284.     fPaletteView = aPaletteView;    
  285.             // save reference to palette in document
  286.     aTextView = new TTextView;
  287.     FailNIL(aTextView);
  288.     aTextView->ITextView(this);
  289.     fTextView = aTextView;    
  290.             // save reference to text view in document
  291.     fTextView->StuffText(fTextHdl);                // so view uses the same characters
  292.     aStdHandler = new TStdPrintHandler;
  293.             // make text view printable
  294.     FailNIL(aStdHandler);
  295.     aStdHandler->IStdPrintHandler (this, aTextView,
  296.          !kSquareDots, kFixedSize, !kFixedSize); }
  297.  
  298. pascal void TTEDocument::DoMakeWindows()
  299. {    TWindow    *aWindow;
  300.     aWindow = NewPaletteWindow(kWindID, 
  301.         kWantHScrollBar, kWantVScrollBar,  this, 
  302.         fTextView, fPaletteView, kPaletteHeight, 
  303.         kTopPalette);                    aWindow->AdaptToScreen();    
  304.             // adjust for various size monitors
  305.     aWindow->SimpleStagger(kStdStaggerAmount, 
  306.         kStdStaggerAmount, &gStdStaggerCount); }
  307.  
  308. pascal void CalcDiskSpace(TBox *aBox, 
  309.         CalcDiskSpaceStruct *aCalcDiskSpaceStruct)
  310. {    aBox->NeedDiskSpace
  311.     (&(aCalcDiskSpaceStruct->myDataForkBytes)); }
  312.  
  313. pascal void TTEDocument::DoNeedDiskSpace
  314.         (long *dataForkBytes, long *rsrcForkBytes)
  315. {    long textBytes, numberOfShapesBytes, 
  316.             cmdNumberBytes;
  317.     CalcDiskSpaceStruct aCalcDiskSpaceStruct;
  318. /*    file format:
  319.         TextCmd Number (integer)
  320.         # shapes (integer)
  321.         data for shape #1
  322.         data for shape #2
  323.         etc.
  324.         text        */
  325.     inherited::DoNeedDiskSpace (dataForkBytes, 
  326.                                     rsrcForkBytes);
  327.     aCalcDiskSpaceStruct.myDataForkBytes = 
  328.                                     *dataForkBytes;
  329.     cmdNumberBytes = sizeof(int);
  330.     numberOfShapesBytes = sizeof(fTextColorCmd);
  331.     ForEachShapeDo((DoToObject)CalcDiskSpace,
  332.                             &aCalcDiskSpaceStruct);
  333.     textBytes = GetHandleSize(fTextHdl);
  334.     *dataForkBytes = cmdNumberBytes + 
  335.         numberOfShapesBytes +  
  336.         aCalcDiskSpaceStruct.myDataForkBytes + 
  337.         textBytes;  }
  338.  
  339. pascal void TTEDocument::DoRead(short aRefNum,
  340.                 Boolean rsrcExists, Boolean forPrinting) 
  341. {    long        textColorCmdNumberBytes;
  342.     int        textColorCmdNumber;        
  343.     long        numberOfShapesBytes;
  344.     int        numberOfShapes;    
  345.     TBox    *aBox;
  346.     long    eof, fPos, textBytes;
  347.     
  348. /*    file format see above    */
  349.     inherited::DoRead(aRefNum, rsrcExists, forPrinting);
  350.                             // print info from disk
  351.     textColorCmdNumberBytes = 
  352.         sizeof(textColorCmdNumber); // read text color
  353.     FailOSErr(
  354.         FSRead(aRefNum, &textColorCmdNumberBytes, 
  355.                         (Ptr) &textColorCmdNumber));
  356.     fTextColorCmd = textColorCmdNumber;
  357.     numberOfShapesBytes = sizeof(numberOfShapes);
  358.                                     // read # items
  359.     FailOSErr(
  360.         FSRead(aRefNum, &numberOfShapesBytes, 
  361.                         (Ptr) &numberOfShapes));
  362.     for (int i = 1; i <= numberOfShapes; i++)
  363.     {    aBox = new TBox; FailNIL(aBox); // make new Box
  364.         aBox->Read(aRefNum); // read & initialize object
  365.         AddShape(aBox); };     // add to list in doc
  366.     
  367.     FailOSErr(GetFPos(aRefNum, &fPos)); 
  368.                             // get size of text in file
  369.     FailOSErr(GetEOF(aRefNum, &eof));
  370.                             // get size of data in file
  371.     textBytes = eof - fPos;
  372.     SetHandleSize(fTextHdl, textBytes);
  373.     HLock (fTextHdl);
  374.     FailOSErr(FSRead(aRefNum, &textBytes, *fTextHdl));    HUnlock (fTextHdl);    }
  375.  
  376. pascal void DoToRect
  377.     (TBox *aBox, DoToRectStruct    *aDoToRectStruct)
  378. {    aBox->Write(aDoToRectStruct->myRefNum);  }
  379.     
  380. pascal void TTEDocument::DoWrite
  381.             (short aRefNum, Boolean makingCopy)
  382. {    long    textColorCmdNumberBytes;
  383.     int        textColorCmdNumber;    
  384.     long    numberOfShapesBytes;
  385.     int        numberOfShapes;
  386.     long    textBytes;
  387.     DoToRectStruct    aDoToRectStruct;
  388.     
  389. /*    file format see above    */
  390.     
  391.     inherited::DoWrite(aRefNum, makingCopy);
  392.                                 // print info to disk
  393.     textColorCmdNumber = fTextColorCmd;
  394.                         // write text color to disk
  395.     textColorCmdNumberBytes = 
  396.                         sizeof(textColorCmdNumber);
  397.     FailOSErr(
  398.         FSWrite(aRefNum, &textColorCmdNumberBytes, 
  399.                         (Ptr) &textColorCmdNumber));
  400.     
  401.     numberOfShapes = fShapeList->fSize;
  402.                         // write # items to disk
  403.     numberOfShapesBytes = sizeof(numberOfShapes);
  404.     FailOSErr(
  405.         FSWrite(aRefNum, &numberOfShapesBytes, 
  406.                         (Ptr) &numberOfShapes));
  407.     aDoToRectStruct.myRefNum = aRefNum;
  408.     ForEachShapeDo((DoToObject)DoToRect,
  409.         &aDoToRectStruct);    // write each rect to disk
  410.     textBytes = GetHandleSize(fTextHdl);
  411.     HLock (fTextHdl);
  412.     FailOSErr(FSWrite(aRefNum, &textBytes, *fTextHdl));
  413.                             // write the text
  414.     HUnlock (fTextHdl); }
  415.  
  416. pascal void TTEDocument::ForEachShapeDo
  417.  (pascal void (*DoToItem)(TObject *item,
  418.   void *DoToItem_Staticlink),void *DoToItem_Staticlink)
  419. {  fShapeList->Each(DoToItem,DoToItem_Staticlink); }
  420.  
  421. pascal void DisposeRect(TBox *aBox, void *link)
  422. {    aBox->Free();    }
  423.  
  424. pascal void TTEDocument::Free()
  425. {    void *link;
  426.     if (fTextHdl != nil) DisposHandle(fTextHdl);
  427.                     // dispose of ASCII characters
  428.     ForEachShapeDo((DoToObject)DisposeRect,link);
  429.                     // dispose of Box objects
  430.     fShapeList->Free();    // dispose of List object
  431.     inherited::Free();    }    // dispose of document object
  432.  
  433. #ifdef qDebug
  434. pascal void TTEDocument::Fields
  435.     (pascal void (*DoToField) (StringPtr fieldName,
  436.      Ptr fieldAddr, short fieldType, void *link), void *link)
  437. {    DoToField("\pTTEDocument", nil, bClass, link);
  438.     DoToField("\pfPaletteView", (Ptr) &fPaletteView, 
  439.                                 bObject, link);
  440.   DoToField("\pfTextView", (Ptr) &fTextView, bObject, link);
  441.   DoToField("\pfTextHdl", (Ptr) &fTextHdl, bHandle, link);
  442.     DoToField("\pfTextColorCmd", (Ptr) &fTextColorCmd, 
  443.                                 bInteger, link);
  444.     DoToField("\pfShapeList", (Ptr) &fShapeList, 
  445.                                 bObject, link);
  446.     inherited::Fields(DoToField, link); }
  447. #endif
  448.  
  449. pascal void 
  450.     TTEDocument::SetTextColorCmd(int theColorCmd)
  451. {    fTextColorCmd = theColorCmd;    }
  452.  
  453. pascal int  TTEDocument::TextColorCmd()
  454. {    return fTextColorCmd;        }
  455.  
  456. //
  457. // ---- TPaletteView ----
  458. //
  459.  
  460. pascal void TPaletteView::IPaletteView
  461.             (TTEDocument *itsTEDocument)
  462. {    VPoint    itsSize;
  463.     SetVPt(&itsSize, kPaletteWidth, kPaletteHeight);
  464.     IView(itsTEDocument, nil, &gZeroVPt, &itsSize,
  465.              sizeFixed, sizeFixed);
  466.     fIconSelected = kText-kBox;  }
  467.  
  468. pascal void TPaletteView::Draw(Rect *area)
  469. {    Rect    aFrame;    Point    aPenSize;
  470.     Handle    aHandle;
  471.     ForeColor(kPaletteColor);    
  472.     aHandle = GetIcon(kBoxIconID);
  473.     FailNILResource(aHandle);
  474.     PlotIcon(&gIconRect[kBox-kBox], aHandle);
  475.     aHandle = GetIcon(kTextIconID);
  476.     FailNILResource(aHandle);
  477.     PlotIcon(&gIconRect[kText-kBox], aHandle);
  478.     ForeColor(blackColor);
  479.     GetQDExtent(&aFrame);
  480.     SetPt(&aPenSize, 1, 1);
  481.     Adorn(&aFrame, aPenSize, adnLineBottom); }
  482.  
  483. pascal void TPaletteView::DoHighlightSelection
  484.                     (HLState fromHL, HLState toHL)
  485. {    Rect    aRect;
  486.     aRect = gIconRect[fIconSelected];
  487.     InsetRect(&aRect, 1, 1);
  488.     SetHLPenState(fromHL, toHL);
  489.     PaintRect(&aRect);}
  490.  
  491. pascal struct TCommand 
  492.     *TPaletteView::DoMouseCommand(Point *theMouse,
  493.                     EventInfo *info, Point *hysteresis)
  494. {    int    index;
  495.     index = int(theMouse->h / kIconWidth);
  496.  
  497.     if ((index < 2) && (index != fIconSelected)) 
  498.     {    if (Focus()) { DoHighlightSelection(hlOn, hlOff); };
  499.         fIconSelected = index;
  500.         if (Focus()) { DoHighlightSelection(hlOff, hlOn); };
  501.     };
  502.     return gNoChanges; }
  503.  
  504. #ifdef qDebug
  505. pascal void TPaletteView::Fields
  506.     (pascal void (*DoToField) (StringPtr fieldName,
  507.     Ptr fieldAddr, short fieldType, void *link), void *link)
  508. {    DoToField("\pTPaletteView", nil, bClass, link);
  509.     DoToField("\pfIconSelected", (Ptr) &fIconSelected, 
  510.                                     bInteger, link);
  511.     inherited::Fields(DoToField, link); }
  512. #endif
  513.  
  514. pascal void 
  515. TTextView::ITextView(TTEDocument *itsTEDocument)
  516. {    VPoint    itsSize;        Rect     itsInset;
  517.     TextStyle    aStyle;
  518.     SetVPt(&itsSize, 100, 100);
  519.     SetRect(&itsInset, 10, 8, 10, 0);
  520.     SetTextStyle
  521.         (&aStyle, applFont, bold, 12, &gRGBBlack);
  522.     ITEView(itsTEDocument, nil, &gZeroVPt, &itsSize, 
  523.         sizePage, sizeFillPages, &itsInset, &aStyle, 
  524.         teJustLeft, !kWithStyle, true);    
  525.     fTEDocument = itsTEDocument;
  526.     fPaletteView = itsTEDocument->fPaletteView;
  527.     fUpdated = true; }
  528.  
  529. pascal Boolean TTextView::DoIdle(IdlePhase phase)
  530. {    if (!fUpdated)
  531.     {    if (Focus()) { DrawContents(); fUpdated = true; };
  532.         return inherited::DoIdle(phase); }
  533. }
  534.  
  535. pascal struct TCommand *TTextView::DoKeyCommand
  536.     (short ch, short aKeyCode, EventInfo *info)
  537. {    int aQDColor;
  538.     aQDColor = 
  539.     gColorArray[fTEDocument->TextColorCmd()-cBlack];
  540.     ForeColor(aQDColor);
  541.     fUpdated = false;
  542.     ForeColor(blackColor);
  543.     return 
  544.         inherited::DoKeyCommand(ch, aKeyCode, info); }
  545.  
  546. pascal void TTextView::DoSetupMenus()
  547. {     int colorIndex, aColorCmd;
  548.     inherited::DoSetupMenus();
  549.     aColorCmd = fTEDocument->TextColorCmd();
  550.     for (colorIndex = cBlack; 
  551.                 colorIndex <= cWhite; colorIndex++)
  552.         EnableCheck(colorIndex, TRUE, 
  553.                     (colorIndex == aColorCmd));  }
  554.  
  555. pascal struct TCommand *TTextView::DoMenuCommand
  556.                         (CmdNumber aCmdNumber)
  557. {    TColorCmd *aColorCmd;
  558.     switch (aCmdNumber)
  559.     {     case cBlack:        case cBlue:     case cGreen: 
  560.         case cRed:        case cWhite: 
  561.             aColorCmd = new TColorCmd;
  562.             FailNIL(aColorCmd);
  563.             aColorCmd->IColorCmd
  564.                 (aCmdNumber, fTEDocument, this);
  565.             return aColorCmd;
  566.         default:    return inherited::DoMenuCommand
  567.                                 (aCmdNumber); }
  568. }
  569.  
  570. pascal struct TCommand *TTextView::DoMouseCommand
  571.     (Point *theMouse,  EventInfo *info, Point *hysteresis)
  572. {     TSketcher *aSketcher;
  573.     if (fPaletteView->fIconSelected == kBox-kBox) 
  574.     {    aSketcher = new TSketcher;
  575.         FailNIL(aSketcher);
  576.         aSketcher->ISketcher(fTEDocument, this);
  577.         return aSketcher; }
  578.     else        return inherited::DoMouseCommand
  579.                         (theMouse,info,hysteresis); }
  580.  
  581. pascal Boolean TTextView::DoSetCursor
  582.             (Point localPoint, RgnHandle cursorRgn)
  583. {    Rect qdExtent;
  584.     GetQDExtent(&qdExtent);
  585.     RectRgn(cursorRgn, &qdExtent);
  586.     if (fPaletteView->fIconSelected == kText-kBox)
  587.     return inherited::DoSetCursor(localPoint, cursorRgn);
  588.     else        SetCursor(*GetCursor(crossCursor));    return true;    }
  589.  
  590. pascal void DrawYourself(TBox *aBox, void *link)
  591. {    aBox->DrawShape();    }
  592.  
  593. pascal void TTextView::Draw(Rect *area)
  594. {    int aQDColor;    void *link;    
  595.     aQDColor = 
  596.     gColorArray[fTEDocument->TextColorCmd()-cBlack];
  597.     ForeColor(aQDColor);    // set text color
  598.     inherited::Draw(area);    // let TTEView draw the text
  599.     PenNormal();
  600.     ForeColor(kBoxColor);    // set box color
  601.     fTEDocument->ForEachShapeDo
  602.                 ((DoToObject)DrawYourself,link);
  603.     ForeColor(blackColor);    }
  604.  
  605. #ifdef qDebug
  606. pascal void TTextView::Fields
  607.     (pascal void (*DoToField) (StringPtr fieldName,
  608.      Ptr fieldAddr, short fieldType, void *link), void *link)
  609. {    DoToField("\pTTextView", nil, bClass, link);
  610.     DoToField("\pfTEDocument", (Ptr) &fTEDocument, 
  611.                                     bObject, link);
  612.     DoToField("\pfPaletteView", (Ptr) &fPaletteView, 
  613.                                     bObject, link);
  614.   DoToField("\pfUpdated", (Ptr) &fUpdated, bBoolean, link);
  615.     inherited::Fields(DoToField, link); }
  616. #endif
  617.  
  618. pascal void TColorCmd::IColorCmd (int aCmdNumber, 
  619.         TTEDocument *itsDocument, TTextView *itsView)
  620. {    TScroller *aScroller;    int oldColorCmd;
  621.     fTextView = itsView;
  622.     fTEDocument = itsDocument;
  623.     aScroller = itsView->GetScroller(true);
  624.     ICommand
  625.         (aCmdNumber, itsDocument, itsView, aScroller);
  626.     oldColorCmd = itsDocument->TextColorCmd(); 
  627.     fOldColorCmd = oldColorCmd;
  628.     fNewColorCmd = aCmdNumber;     }
  629.  
  630. pascal void TColorCmd::DoIt()
  631. {    fTEDocument->SetTextColorCmd(fNewColorCmd);
  632.     fTextView->ForceRedraw();    }
  633.  
  634. pascal void TColorCmd::RedoIt()    {    UndoIt();  }
  635.  
  636. pascal void TColorCmd::UndoIt()
  637. {    fNewColorCmd = fOldColorCmd;
  638.     fOldColorCmd = fTEDocument->TextColorCmd();
  639.     DoIt();    }
  640.  
  641. #ifdef qDebug
  642. pascal void TColorCmd::Fields
  643.     (pascal void (*DoToField) (StringPtr fieldName,
  644.      Ptr fieldAddr, short fieldType, void *link), void *link)
  645. {    DoToField("\pTColorCmd", nil, bClass, link);
  646.     DoToField("\pfTEDocument", (Ptr) &fTEDocument, 
  647.                                     bObject, link);
  648.   DoToField("\pfTextView", (Ptr) &fTextView, bObject, link);
  649.     DoToField("\pfOldColorCmd", (Ptr) &fOldColorCmd, 
  650.                                     bInteger, link);
  651.     DoToField("\pfNewColorCmd", (Ptr) &fNewColorCmd, 
  652.                                     bInteger, link);
  653.     inherited::Fields(DoToField, link);  }
  654. #endif
  655.  
  656. pascal void TSketcher::ISketcher
  657.     (TTEDocument *itsDocument, TTextView *itsView)
  658. {    TScroller *aScroller;
  659.     aScroller = itsView->GetScroller(true);
  660.     ICommand
  661.         (cDrawBox, itsDocument, itsView, aScroller);
  662.     fTEDocument = itsDocument; fTextView = itsView;  }
  663.  
  664. pascal struct TCommand  *TSketcher::TrackMouse
  665.     (TrackPhase aTrackPhase, VPoint *anchorPoint, 
  666.     VPoint *previousPoint,  VPoint *nextPoint, 
  667.     Boolean mouseDidMove)
  668. {    Rect    newRect;    TBox    *aBox;
  669.     if (aTrackPhase == trackRelease)
  670.     { Pt2Rect(fTextView->ViewToQDPt(anchorPoint), 
  671.         fTextView->ViewToQDPt(nextPoint), &newRect);
  672.         aBox = new TBox; FailNIL(aBox);
  673.         aBox->IBox(&newRect); fBox = aBox;
  674.         fBoxLocation = newRect;    }
  675.     return this;    }
  676.  
  677. pascal void TSketcher::DoIt()
  678. {    fTEDocument->AddShape(fBox);
  679.     fTextView->InvalidRect(&fBoxLocation);  }
  680.  
  681. pascal void TSketcher::RedoIt()  {  DoIt();  }
  682.  
  683. pascal void TSketcher::UndoIt()
  684. {    fTEDocument->DeleteShape();
  685.     fTextView->InvalidRect(&fBoxLocation);  }
  686.  
  687. #ifdef qDebug
  688. pascal void TSketcher::Fields
  689.     (pascal void (*DoToField) (StringPtr fieldName,
  690.      Ptr fieldAddr, short fieldType, void *link), void *link)
  691. {    DoToField("\pTSketcher", nil, bClass, link);
  692.     DoToField("\pfTEDocument", (Ptr) &fTEDocument, 
  693.                                     bObject, link);
  694.   DoToField("\pfTextView", (Ptr) &fTextView, bObject, link);
  695.     DoToField("\pfBox", (Ptr) &fBox, bObject, link);
  696.     DoToField("\pfBoxLocation", (Ptr) &fBoxLocation, 
  697.                                     bRect, link);
  698.     inherited::Fields(DoToField, link);  }
  699. #endif
  700.  
  701. pascal void TBox::IBox(Rect *itsLocation)
  702.     {  fLocation = *itsLocation;  }
  703.  
  704. pascal void TBox::DrawShape()
  705. {    PenSize(4,4);
  706.     FrameRoundRect(&fLocation, 20,20); }
  707.  
  708. pascal void TBox::NeedDiskSpace(long *data)
  709. {    data = data + sizeof(fLocation);  }
  710.  
  711. pascal void TBox::Read(short aRefNum)
  712. {    long bytes;
  713.     bytes = sizeof(fLocation);
  714.     FailOSErr(
  715.         FSRead(aRefNum, &bytes, (Ptr) &fLocation));  }
  716.  
  717. pascal void TBox::Write(short aRefNum)
  718. {    long bytes;
  719.     bytes = sizeof(fLocation);
  720.     FailOSErr(
  721.         FSWrite(aRefNum, &bytes, (Ptr) &fLocation));}
  722.  
  723. #ifdef qDebug
  724. pascal void TBox::Fields
  725.     (pascal void (*DoToField) (StringPtr fieldName,
  726.      Ptr fieldAddr, short fieldType, void *link), void *link)
  727. {    DoToField("\pTBox", nil, bClass, link);
  728.     DoToField("\pfLocation", (Ptr) &fLocation, bRect, link);
  729.     inherited::Fields(DoToField, link); }
  730. #endif
  731.  
  732. TTEApplication *gTEApplication;
  733. int main()    
  734. {    InitToolBox();
  735.     if (ValidateConfiguration(&gConfiguration))
  736.     {    InitUMacApp(8); InitUPrinting(); InitUTEView();
  737.         gTEApplication = new TTEApplication;
  738.         FailNIL(gTEApplication);
  739.         gTEApplication->ITEApplication(kFileType);
  740.         gTEApplication->Run();  }
  741.     else StdAlert(phUnsupportedConfiguration);
  742.     return 0;  }
  743.  
  744. Listing 3: TEDemo.r (Resource definitions)
  745.  
  746. #ifndef __TYPES.R__
  747. #include "Types.r"
  748. #endif
  749.  
  750. #ifndef __SYSTYPES.R__
  751. #include "SysTypes.r"
  752. #endif
  753.  
  754. #ifndef __MacAppTypes__
  755. #include "MacAppTypes.r"
  756. #endif
  757.  
  758. #ifndef __ViewTypes__
  759. #include "ViewTypes.r"
  760. #endif
  761.  
  762. #if qDebug
  763. include "Debug.rsrc";
  764. #endif
  765.  
  766. include "MacApp.rsrc";
  767. include "Printing.rsrc";
  768. include "TEOther.rsrc";    
  769.     /* DLOG & DITL 1001; ICON 256,257; ICN# 128,129 */
  770. include "TEDemo" 'CODE';
  771.  
  772. #define cHelp         1001
  773. #define cDrawBox    2001
  774. #define cBlack         5001
  775. #define cBlue         5002
  776. #define cGreen         5003
  777. #define cRed         5004
  778. #define cWhite         5005
  779.  
  780. #define kSignature    'JLMT'
  781. #define kDocFileType    'JL01'
  782. #define getInfoString    
  783. "©1990 J.Langowski/MacTutor. Translated from MacApp® Pascal."
  784.  
  785. resource 'WIND' (1001, purgeable) {
  786.     {50, 20, 250, 450}, zoomDocProc, invisible, goAway,
  787.     0x0, "<<<>>>"  };
  788.  
  789. resource 'DITL' (201, purgeable) {
  790.     /* About box */
  791.      {     /* [1] */
  792.         {130, 182, 150, 262},
  793.         Button { enabled, "OK" };
  794.         /* [2] */
  795.         {10, 80, 110, 270},
  796.         StaticText { disabled,
  797.             "Displays text and boxes"
  798.             "\n\nThis program was written "
  799.     "with MacApp® © 1985-1989 Apple Computer, Inc." };
  800.         /* [3] */
  801.         {10, 20, 42, 52},
  802.         Icon { disabled, 1 }    }    };
  803.  
  804. resource 'ALRT' (201, purgeable) {
  805.     {90, 100, 250, 412},
  806.     201,
  807.     {    OK, visible, silent;    OK, visible, silent;
  808.         OK, visible, silent;    OK, visible, silent  }   };
  809.  
  810. resource 'cmnu' (1) {
  811.     1, textMenuProc, 0x7FFFFFFB, enabled, apple,
  812.      {     "About TEDemo…", 
  813.             noIcon, noKey, noMark, plain, cAboutApp;
  814.         "Help…", noIcon, "H", noMark, plain, cHelp;
  815.         "-", noIcon, noKey, noMark, plain, nocommand } };
  816.  
  817. resource 'cmnu' (2) {
  818.     2, textMenuProc, allEnabled, enabled, "File",
  819.      {     "New", noIcon, "N", noMark, plain, 10;
  820.         "Open…", noIcon, "O", noMark, plain, 20;
  821.         "-", noIcon, noKey, noMark, plain, nocommand;
  822.         "Close", noIcon, noKey, noMark, plain, 31;
  823.         "Save", noIcon, "S", noMark, plain, 30;
  824.         "Save As…", noIcon, noKey, noMark, plain, 32;
  825.         "Save a Copy In…", 
  826.                     noIcon, noKey, noMark, plain, 33;
  827.         "-", noIcon, noKey, noMark, plain, nocommand;
  828.         "Page Setup…", 
  829.                     noIcon, noKey, noMark, plain, 176;
  830.         "Print One", noIcon, "P", noMark, plain, 177;
  831.         "Print…", noIcon, noKey, noMark, plain, 178;
  832.         "-", noIcon, noKey, noMark, plain, nocommand;
  833.         "Quit", noIcon, "Q", noMark, plain, 36  }   };
  834.  
  835. resource 'cmnu' (3) {
  836.     3, textMenuProc, allEnabled, enabled, "Edit",
  837.      {     "Undo", noIcon, "Z", noMark, plain, 101;
  838.         "-", noIcon, noKey, noMark, plain, nocommand;
  839.         "Cut", noIcon, "X", noMark, plain, 103;
  840.         "Copy", noIcon, "C", noMark, plain, 104;
  841.         "Paste", noIcon, "V", noMark, plain, 105;
  842.         "Clear", noIcon, noKey, noMark, plain, 106;
  843.         "-", noIcon, noKey, noMark, plain, nocommand;
  844.         "Show Clipboard", 
  845.                 noIcon, noKey, noMark, plain, 35  }  };
  846.  
  847. resource 'cmnu' (4) {
  848.     4, textMenuProc, allEnabled, enabled, "TextColor",
  849.      {     "Black", noIcon, noKey, noMark, plain, cBlack;
  850.         "Blue", noIcon, "B", noMark, plain, cBlue;
  851.         "Green", noIcon, "G", noMark, plain, cGreen;
  852.         "Red", noIcon, "R", noMark, plain, cRed;
  853.         "White", noIcon, noKey, noMark, outline, cWhite }
  854. };
  855.  
  856. resource 'cmnu' (128) {
  857.     128,textMenuProc,allEnabled,enabled,"Buzzwords",
  858.      {    "Page Setup Change", 
  859.     noIcon, noKey, noMark, plain, cChangePrinterStyle;
  860.         "Typing", noIcon, noKey, noMark, plain, cTyping;
  861.         "Drawing",  
  862.             noIcon, noKey, noMark, plain, cDrawBox }  };
  863.  
  864. resource 'MBAR' (128) {    {1; 2; 3; 4}    };
  865.  
  866. resource 'mctb' (128) {
  867.     {     /* Blue */
  868.         4, 2,
  869.         {    0x0000, 0x0000, 0xFFFF;
  870.             0x0000, 0x0000, 0xFFFF;
  871.             0x0000, 0x0000, 0xFFFF;
  872.             0xFFFF, 0xFFFF, 0xFFFF    };
  873.         /* Green */
  874.         4, 3,
  875.         {     0x0000, 0xDB00, 0x0000;                0x0000, 0xDB00, 0x0000;                0x0000, 0xDB00, 0x0000;                0xFFFF, 0xFFFF, 0xFFFF    };
  876.         /* Red */
  877.         4, 4,
  878.         {    0xDB00, 0x0000, 0x0000;                0xDB00, 0x0000, 0x0000;                0xDB00, 0x0000, 0x0000;                0xFFFF, 0xFFFF, 0xFFFF    }   }   };
  879.  
  880. resource 'SIZE' (-1) {
  881.     saveScreen, acceptSuspendResumeEvents,
  882.     enableOptionSwitch, canBackground,
  883.     MultiFinderAware, backgroundAndForeground,
  884.     dontGetFrontClicks, ignoreChildDiedEvents,
  885.     is32BitCompatible, reserved, reserved, reserved,
  886.     reserved, reserved, reserved, reserved,
  887. #if qDebug
  888.     640 * 1024, 512 * 1024
  889. #else
  890.     240 * 1024, 220 * 1024
  891. #endif 
  892. };
  893.  
  894. resource 'seg!' (256, purgeable) {
  895.     {    "GClose"; "GDoCommand"; "GNonRes";
  896.         "GFile"; "GOpen"; "GSelCommand"; }  };
  897.  
  898. resource 'mem!' (256, purgeable) {
  899.     30 * 1024,    /* Add to temporary reserve */
  900.     0,            /* Add to permanent reserve */
  901.     0            /* Add to stack space */
  902. };
  903.  
  904. /* Bundle */
  905.  
  906. type kSignature as 'STR ';
  907.  
  908. resource kSignature (0) { getInfoString; };
  909. resource 'FREF' (128) { 'APPL', 0, "TEDemo };
  910. resource 'FREF' (129) { kDocFileType, 1, "TEDemo" };
  911. resource 'BNDL' (128) { kSignature,     0,
  912.         {    'ICN#',    { 0, 128; 1, 129 };
  913.             'FREF',    { 0, 128; 1, 129 } }   };
  914.  
  915. RESOURCE 'vers' (2,
  916. #if qNames
  917. "Package Version",
  918. #endif
  919.     purgeable) { 0x02, 0x00, beta, 0x06, verUs, "2.0ß9",
  920.     "MacApp® 2.0ß9, ©Apple Computer, Inc. 1989" };
  921. RESOURCE 'vers' (1,
  922. #if qNames
  923. "File Version",
  924. #endif
  925.     purgeable) { 0x01,0x00,beta,0x05,verUs,"TEDemo",
  926.     "v 0.0, ©JL/MacTutor 1990"  };
  927.  
  928. /* debug window */
  929. resource 'dbug' (kDebugParamsID,
  930. #if qNames
  931. "Debug",
  932. #endif
  933.     purgeable) {
  934.         {350, 4, 474, 636}, /* Bounds rect */
  935.         1,         /* font rsrc ID (normal = monaco) */
  936.         9,        /* font size (normal = 9) */
  937.         100,    /* Number of lines */
  938.         100,    /* Width of lines in characters */
  939.         true,    /* open initially */
  940.         "Jörg's Debug Window"  /* Window title */   };
  941.  
  942. Listing 4: TEDemo.MAMake (MacApp Make file)
  943.  
  944. #----------------------------------------------------------------------------AppName = TEDemo
  945. #----------------------------------------------------------------------------#    List resource files that the Rez file includes
  946. OtherRsrcFiles = ∂
  947.     "{MAObj}Printing.rsrc"    ∂
  948.     "TEOther.rsrc"
  949.